<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          v-hasPermi="['11810']"
          @click="handleAdd"
          >添加讲师</el-button
        >
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="dataList" :border="true">
      <!-- <el-table-column label="讲师ID" align="center" prop="dictId" /> -->
      <el-table-column label="讲师类型" align="center" prop="typeVal" />
      <el-table-column label="讲师姓名" align="center" prop="thename" />
      <el-table-column align="center" prop="headPic" label="讲师头像">
        <template slot-scope="scope">
          <imagePreview :src="scope.row.headPic" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="讲师级别" align="center" prop="levelVal" />
      <el-table-column label="操作" align="center" width="100">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            v-hasPermi="['11820']"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            v-hasPermi="['11830']"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.currentPage"
      :limit.sync="queryParams.pagesize"
      @pagination="getList"
    />
    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- *【讲师类型】（下拉框：内训老师、员工讲师、外聘老师）、*老师姓名、*老师头像（照片）、*【老师级别】（初级、中级、高级）、顺序号（默认：0） -->
        <el-form-item label="讲师类型" prop="typeId">
          <el-radio-group v-model="form.typeId" @change="changeType">
            <el-radio
              v-for="item in typeOptions"
              :key="item.id"
              :value="item.id"
              :label="item.id"
              >{{ item.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="讲师姓名" prop="thename">
          <el-input v-model="form.thename" placeholder="请输入讲师姓名" />
        </el-form-item>
        <el-form-item label="讲师头衔" prop="exclusiveTitle">
          <el-input
            v-model="form.exclusiveTitle"
            placeholder="请输入讲师头衔"
          />
        </el-form-item>
        <el-form-item label="从业经历" prop="workingExperience">
          <el-input
            type="textarea"
            v-model="form.workingExperience"
            placeholder="请输入从业经历"
          />
        </el-form-item>
        <el-form-item label="资质证书" prop="qualificationCert">
          <el-input
            type="textarea"
            v-model="form.qualificationCert"
            placeholder="请输入资质证书"
          />
        </el-form-item>
        <el-form-item label="擅长方向" prop="goodDirection">
          <el-input
            type="textarea"
            v-model="form.goodDirection"
            placeholder="请输入擅长方向"
          />
        </el-form-item>
        <el-form-item label="讲师性别" prop="genderId">
          <el-radio-group v-model="form.genderId" @change="changeGender">
            <el-radio
              v-for="item in genderOptions"
              :key="item.id"
              :value="item.id"
              :label="item.id"
              >{{ item.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="讲师头像" prop="headPic">
          <uploadImage
            :theType="7"
            v-model="form.headPic"
            @isUploaded="isUploadeds"
          />
        </el-form-item>
        <el-form-item label="讲师级别" prop="levelId">
          <el-radio-group v-model="form.levelId" @change="changeLevel">
            <el-radio
              v-for="item in levelOptions"
              :key="item.id"
              :value="item.id"
              :label="item.id"
              >{{ item.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="位置排序" prop="sort">
          <el-input-number
            v-model="form.sort"
            controls-position="right"
            :min="0"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import UploadImage from "@/components/ImageUpload";
import {
  getPage,
  listData,
  getData,
  delData,
  addData,
  updateData,
  delMultiData,
  getSelect,
  getEnable,
} from "@/api/system/teacherList";

export default {
  name: "teacherList",
  components: {
    UploadImage,
  },
  data() {
    return {
      loading: true,
      dataList: [],
      title: "",
      open: false,
      form: {},
      queryParams: {
        currentPage: 1,
        pagesize: 10,
      },
      total: 0,
      rules: {
        typeId: [
          { required: true, message: "请选择讲师类型", trigger: "change" },
        ],
        thename: [
          { required: true, message: "讲师姓名不能为空", trigger: "blur" },
        ],
        exclusiveTitle: [
          { required: true, message: "头衔不能为空", trigger: "blur" },
        ],
        workingExperience: [
          { required: true, message: "从业经历不能为空", trigger: "blur" },
        ],
        qualificationCert: [
          { required: true, message: "资质证书不能为空", trigger: "blur" },
        ],
        goodDirection: [
          { required: true, message: "擅长方向不能为空", trigger: "blur" },
        ],
        headPic: [
          { required: true, message: "讲师头像不能为空", trigger: "blur" },
        ],
        levelId: [
          { required: true, message: "请选择讲师级别", trigger: "change" },
        ],
        genderId: [
          { required: true, message: "请选择性别", trigger: "change" },
        ],
      },
      levelOptions: [],
      typeOptions: [],
      genderOptions: [],
      currLevel: {},
      currTheType: {},
      currGender: {},
      isUploadedByTheUser: false, //头像是否由用户上传
      headPicMale:
        "http://data.aqykt.cn/storage/images/7/20211214/2b4a542f604d3c9d7e40387b554f2b8e.png", //男头像 空头像
      headPicFemale:
        "http://data.aqykt.cn/storage/images/7/20211214/b645eff4946e6824ddce823ce66987cb.png", //女头像 空头像
    };
  },
  watch: {},
  created() {
    this.getList();
    this.getSelect();
  },
  methods: {
    changeType(val) {
      this.form.typeVal = val
        ? this.typeOptions.find((ele) => ele.id === val).label
        : "";
    },
    changeLevel(val) {
      this.form.levelVal = val
        ? this.levelOptions.find((ele) => ele.id === val).label
        : "";
    },
    changeGender(val) {
      this.form.genderVal = val
        ? this.genderOptions.find((ele) => ele.id === val).label
        : "";
      // 切换男/女时，如果头像不是由用户上传     如果头像不是默认头像
      if (val === 1 && this.isUploadedByTheUser === false) {
        //男
        this.form.headPic = this.headPicMale;
      } else if (val === 2 && this.isUploadedByTheUser === false) {
        //女
        this.form.headPic = this.headPicFemale;
      }
    },
    /** 查询字典数据列表 */
    getList() {
      this.loading = true;
      listData(this.queryParams).then((response) => {
        this.dataList = response.result.rows;
        this.total = response.result.total;
        this.loading = false;
      });
    },
    getSelect() {
      getSelect().then((response) => {
        if (response.result.theLevel.length > 0) {
          this.levelOptions = response.result.theLevel; //讲师级别
          this.currLevel = response.result.theLevel[0]; //讲师级别 默认选中
        }
        if (response.result.theType.length > 0) {
          this.typeOptions = response.result.theType; //讲师类型
          this.currTheType = response.result.theType[0];
        }
        if (response.result.gender.length > 0) {
          this.genderOptions = response.result.gender; //性别
          this.currGender = response.result.gender[0];
        }
      });
    },
    // 表单重置
    reset() {
      this.form = {
        thename: undefined,
        exclusiveTitle: undefined, // 专属头衔
        introduction: undefined, //个人简介
        workingExperience: undefined, //从业经历
        qualificationCert: undefined, //资质证书
        goodDirection: undefined, //擅长方向
        headPic: this.headPicMale,
        typeId: this.currTheType.id,
        typeVal: this.currTheType.label,
        genderId: this.currGender.id,
        genderVal: this.currGender.label,
        levelId: this.currLevel.id,
        levelVal: this.currLevel.label,
        sort: 0,
      };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.isUploadedByTheUser = false; //添加 会涉及使用默认头像 “是否是用户上传”字段，需要设置成“用户没有上传”  这样，切换男/女，头像会切换空头像
      this.reset();
      this.form.headPic;
      this.open = true;
      this.title = "添加讲师";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.isUploadedByTheUser = true; //添加 不会涉及使用默认头像，直接使用获取到的数据 “是否是用户上传”字段，需要设置成“用户已经上传”  这样，切换男/女，头像不会切换空头像
      this.reset();
      const theId = row.theid;
      getData(theId).then((response) => {
        this.form = response.result;
        this.open = true;
        this.title = "修改";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.theid != undefined) {
            updateData(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addData(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const theId = row.theid;
      this.$confirm("是否确认删除?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delData(theId);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    isUploadeds(val) {
      console.log("val", val);
      this.isUploadedByTheUser = true;
    },
  },
};
</script>

<style scoped lang="scss">
/deep/ .el-textarea__inner {
  height: 33px;
}
/* /deep/ .el-table {
  margin-left: 100px;
} */
/* treeselect */
.vue-treeselect__input {
  display: block;
}
/deep/ .vue-treeselect__control {
  height: 32px;
  line-height: 32px;
  margin-top: 3px;
}
</style>